package com.itheima.health.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.health.dao.MenuDao;
import com.itheima.health.dao.RoleDao;
import com.itheima.health.dao.UserDao;
import com.itheima.health.pojo.Menu;
import com.itheima.health.pojo.Role;
import com.itheima.health.pojo.User;
import com.itheima.health.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: Created by Wxk
 * @date: 2020/11/21 14:14
 * @description:
 * @version：1.0
 */
@Service
public class MenuServiceImpl implements MenuService {
    @Autowired
    private MenuDao menuDao;
    @Autowired
    private UserDao userDao;
    @Autowired
    private RoleDao roleDao;
    @Override
    @Transactional
    public List<Menu> findMenuListByUsername(String username) {
        List<Menu> menu=new ArrayList<>();
        User user = userDao.selectByUsername(username);
        List<Role> roles = roleDao.selectByUserId(user.getId());
        for (Role role : roles) {
            List<Menu> menu1= menuDao.findParentMenuByRoleId(role.getId());
            for (Menu menu2 : menu1) {
                List<Menu> children = menu2.getChildren();
                children.addAll(menuDao.findChildrenMenuByRoleId(menu2.getId()));
            }

           menu.addAll(menu1);
        }
        return menu;
    }
}
